/**
 *  Date           	Name    		     Change
 *  24/09/2014     	Roy Chan			 Creation
 *
 */
package persistence;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import persistence.model.Post;


/**
 * @author Roy Chan
 *
 */
public class PostDaoImpl extends DaoBaseClass{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void insert(Post post){
		try {
			StringBuilder sql = new StringBuilder();
			sql.append("INSERT INTO post (term, file_id, word_post) ");
			sql.append("VALUES ");
			sql.append("(?,?,?)");
			
			PreparedStatement preSt = this.getConnection().prepareStatement(sql.toString());
			
			preSt.setString(1, post.getTerm());
			preSt.setInt(2, post.getFileID());
			preSt.setInt(3, post.getWordPost());
			
			preSt.executeUpdate();
			this.getConnection().commit();
		} catch (SQLException e) {

			e.printStackTrace();
			
		}
		
	}
	
	
	public void deleteAll(){

		StringBuilder sql = new StringBuilder();
		sql.append("DELETE * FROM post");
		
		try {
			Statement st = this.getConnection().createStatement();
			int delete = st.executeUpdate(sql.toString());
			
			System.out.println("deleted: " + delete);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ResultSet selectAll(){
		ResultSet rs = null;
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM post");
		
		try {
			Statement st = this.getConnection().createStatement();
			rs = st.executeQuery(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rs;
	}

}
